package com.maxd.controller;

import com.google.common.collect.Lists;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.MedalInfo;
import com.maxd.model.RequirementInfo;
import com.maxd.respository.InstructorInfoRepository;
import com.maxd.respository.MedalInfoRepository;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.Date;
import java.util.List;

@Api(tags = "勋章信息管理")
@RestController
@RequestMapping("/medal")
public class MedalInfoController {

    @Autowired
    private MedalInfoRepository medalInfoRepository;

    @PostMapping("/save")
    @ApiOperation("保存或更新勋章信息")
    public Result<Void> save(@Validated @RequestBody MedalInfo medalInfo) {
        AdminUtils.checkLogin();
        if(medalInfo.getId() != null) {
            medalInfo.setUpdateBy(LoginUserHolder.getAdminId());
            medalInfo.setUpdateTime(new Date());
        } else {
            medalInfo.setStatus("1");
            medalInfo.setCreateBy(LoginUserHolder.getAdminId());
            medalInfo.setUpdateBy(LoginUserHolder.getAdminId());
        }
        medalInfoRepository.save(medalInfo);
        return ResultUtil.success();
    }

    @IgnoreSecurity
    @ApiOperation("查询勋章列表")
    @GetMapping("/medal-list")
    public Result<List<MedalInfo>> list(@ApiParam("记录ID") @RequestParam(required = false, name = "id") Long id,
                                         @ApiParam("勋章名称，支持模糊查询") @RequestParam(required = false, name = "name") String name,
                                         @ApiParam("状态 0-失效 1-生效") @RequestParam(required = false, name = "status") String status) {
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        Specification<MedalInfo> medalInfoSpecification = buildCondition(id, name, status);
        List<MedalInfo> medals = medalInfoRepository.findAll(medalInfoSpecification, sort);
        return ResultUtil.success(medals);
    }

    @IgnoreSecurity
    @ApiOperation("分页查询勋章列表")
    @GetMapping("/medal-page")
    public Result<PageRes<MedalInfo>> medalPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                            @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                            @ApiParam("记录ID") @RequestParam(required = false, name = "id") Long id,
                                            @ApiParam("勋章名称，支持模糊查询") @RequestParam(required = false, name = "name") String name,
                                            @ApiParam("状态 0-失效 1-生效") @RequestParam(required = false, name = "status") String status) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "updateTime");
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(order));
        Specification<MedalInfo> medalInfoSpecification = buildCondition(id, name, status);
        Page<MedalInfo> pageList = medalInfoRepository.findAll(medalInfoSpecification, pageable);
        return ResultUtil.success(PageUtils.toPageRes(pageList));
    }

    private Specification<MedalInfo> buildCondition(Long id, String name, String status) {
        Specification<MedalInfo> queryCondition = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = Lists.newArrayList();
            if(id != null) {
                predicates.add(criteriaBuilder.equal(root.get("id"), id));
            }
            if(StringUtils.isNotEmpty(name)) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }
            if(StringUtils.isNotEmpty(status)) {
                predicates.add(criteriaBuilder.equal(root.get("status"), status));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return queryCondition;
    }

}
